<?php
namespace frontend\controllers;

use Codeception\Module\Yii2;
use GK\JavascriptPacker;
use Overtrue\Pinyin\Pinyin;
use yii\web\Controller;
use yii\web\Response;

/**
 * Site controller
 */
class CharController extends Controller
{

    public $enableCsrfValidation = false;

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionSpecialChar()
    {
        return $this->render('index');
    }

    public function actionHtmlEntity()
    {
        return $this->render('htmlentity');
    }

    public function actionFtz()
    {
        return $this->render('ftz');
    }

    public function actionHuoxin()
    {
        return $this->render('huoxin');
    }
    /**
     * 拼音
     */
    public function actionPinyin()
    {

        if (\Yii::$app->request->isAjax) {

            \Yii::$app->response->format = Response::FORMAT_JSON;
            $words = \Yii::$app->request->post('words', '');
            $tone = \Yii::$app->request->post('tone', 1) == 1;
            $name = \Yii::$app->request->post('name', 0) == 1;

            if (trim($words) == '') {
                return ['status' => 1];
            }
            $tone = $tone ? PINYIN_TONE : PINYIN_NO_TONE;
            $pinyin = new Pinyin();
            if ($name) {
                $oj = $pinyin->name($words, $tone);
            } else {
                $oj = $pinyin->convert($words, $tone);
            }
            return ['status' => 0, 'data' => $oj];

        } else {
            return $this->render('pinyin');
        }
    }


    public function actionBishun()
    {
        if (\Yii::$app->request->isPost) {
            $result=$this->bishunDo();
            return $this->renderPartial('bishun-do',$result);
        } else {
            return $this->render('bishun');
        }
    }


    private function bishunDo()
    {

        $words = $_POST['words'] ?? '';//笔顺字

        $bglx = $_POST['types'] ?? 'tzg';//表格类型，默认田字格
        $bgcolor = $_POST['bgcolor'] ?? 'black';//表格颜色

        if ($_POST['bgcolor'] != 'black') {
            $bglx = $bglx . $bgcolor;//表格颜色变化
        }

        $z_color = $_POST['zcolor'] ?? 'black';//主字体颜色
        $f_color = $_POST['fcolor'] ?? '5';//辅字体颜色
        $title = $_POST['title'] ?? '';//辅字体颜色
        $bs = $_POST['bs'] ?? '0';//笔顺填充

        /*过滤掉非中文*/
        preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $words, $words);
        $words = implode('', $words[0]);

        //没有文字，跳转
        if (!$words) {
            header("Location: /");
            exit();
        }

        /*主字体颜色*/
        $color = [
            'green' => '0,176,80',//绿色
            'black' => '0,0,0',//黑色
            'red' => '152,15,41',//红色
        ];

        /*辅字体颜色*/
        $fz_color = [
            '10' => '255,255,255',//白色

            'green1' => '136,255,136',//绿色1
            'green2' => '153,255,153',//绿色2
            'green3' => '160,255,160',//绿色3
            'green4' => '170,255,170',//绿色4
            'green5' => '184,255,184',//绿色5
            'green6' => '204,255,204',//绿色6

            'black1' => '136,136,136',//黑色1
            'black2' => '153,153,153',//黑色2
            'black3' => '160,160,160',//黑色3
            'black4' => '170,170,170',//黑色4
            'black5' => '184,184,184',//黑色5
            'black6' => '204,204,204',//黑色6

            'red1' => '255,136,136',//红色1
            'red2' => '255,153,153',//红色2
            'red3' => '255,160,160',//红色3
            'red4' => '255,170,170',//红色4
            'red5' => '255,184,184',//红色5
            'red6' => '255,204,204',//红色6
        ];

        $color = $color[$z_color];//显示主颜色

        $fcolor = $fz_color[$z_color . $f_color];//辅助颜色

        if ($f_color == '10') {
            $fcolor = $fz_color['10'];
        }

        preg_match_all("/./u", $words, $hz);

        $gz = [];
        $cols = 12;
        $rows = 14;
        $path=\Yii::$app->basePath.DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.'bs'.DIRECTORY_SEPARATOR;

        for ($ihz = 0; $ihz < count($hz['0']); $ihz++) {

            $hzGBK = iconv('UTF-8', 'GB2312', $hz['0'][$ihz]);
            if (file_exists($path."bishun_data/" . $hzGBK . ".json")) {
                $data = file_get_contents($path."bishun_data/" . $hzGBK . ".json");
            } else {
                //没有指定字
                if (!file_exists($path."bishun_data/" . $hz['0'][$ihz] . ".json")) {
                    continue;
                }
                $data = file_get_contents($path."bishun_data/" . $hz['0'][$ihz] . ".json");
            }
            $data = json_decode($data, 1);
            $count = count($data['strokes']);//统计共有多少画

////////////////////////////////////////////
            /*显示完整字符*/
            $tmpGz = '<li class="svg"><svg width="54" height="54" style="margin-top: -11px;"><g transform="translate(-2.9,48) scale(0.058, -0.0572)">';
            foreach ($data['strokes'] as $v) {
                $tmpGz .= '<path d="' . $v . '"style="fill:rgb(' . $color . ');stroke:rgb(' . $color . ');" stroke-width = "0"></path>';
            }
            $tmpGz .= "</g></svg></li>";
            $gz[] = $tmpGz;

////////////////////////////////////////////
            //按笔数显示
            for ($i = 0; $i < $count; $i++) {
                $tmpGz = '<li class="svg"><svg width="54" height="54" style="margin-top: -11px;"><g transform="translate(-2.9,48) scale(0.058, -0.0572)">';
                for ($ii = 0; $ii <= $i; $ii++) {
                    $tmpGz .= '<path d="' . $data['strokes'][$ii] . '"style="fill:rgb(' . $fcolor . ');stroke:rgb(' . $fcolor . ');" stroke-width = "0"></path>';
                }
                $tmpGz .= '</g></svg></li>';
                $gz[] = $tmpGz;
            }


            /*判断是否填充12个田字格*/
            $tzg12 = ($count + 1) / $cols;
            $kg = 0;//空格，每行剩余未填充的空格
            if (!is_int($tzg12)) {
                $kg = $cols - ($cols * $tzg12);
            }
            //为负数
            if ($kg < 0) {
                $kg = ((ceil(abs($kg) / $cols) + 1) * $cols) - ($count + 1);
            }

            /*行数不够，填充*/
            //填充完整字符
            if ($kg and $bs) {
                for ($i = 0; $i < $kg; $i++) {
                    /*显示完整字符*/
                    $tmpGz = '<li class="svg"><svg width="54" height="54" style="margin-top: -11px;"><g transform="translate(-2.9,48) scale(0.058, -0.0572)">';
                    foreach ($data['strokes'] as $v) {
                        $tmpGz .= '<path d="' . $v . '"style="fill:rgb(' . $fcolor . ');stroke:rgb(' . $fcolor . ');" stroke-width = "0"></path>';
                    }
                    $tmpGz .= "</g></svg></li>";
                    $gz[] = $tmpGz;
                }
            }
            //填充空行
            if ($kg and !$bs) {
                for ($i = 0; $i < $kg; $i++) {
                    $gz[] = '<li class="svg">&nbsp;</li>';
                }

            }
        }

        $pageSize = $cols * $rows;
        $pages = ceil(count($gz) / $pageSize);
        $result = '';
        for ($p = 0; $p < $pages; $p++) {
            $result .= "<div class=\"page-head\">{$title}</div><ul>";
            for ($i = 0; $i < $pageSize; $i++) {
                $index = $p * $pageSize + $i;
                if (isset($gz[$index])) {
                    $result .= $gz[$index];
                } else {
                    $result .= "<li>&nbsp;</li>";
                }
            }
            $result .= "</ul><div class='page-foot'><img src='../bs/img/bibei365.png' /><img src='../bs/img/bibei365.png' /><img src='../bs/img/bibei365.png' /></div>";
        }

        return ['content'=>$result,'bglx'=>$bglx];
    }
}
